<?php 
defined('RUN_KATANA') or die('Restricted access');
class ObjectController extends Object
{
	public 	$Settings;
	public	$Forms;
	public 	$Info;
	public 	$Error;
	public  $CoreHandle;
	public  $List;
	public  $ViewObject;
	public  $ViewTemplate;
	public 	$Pager;
	public 	$Search;
	
	public function cronHour()
	{ return; }
	public function cronDay()
	{ return; }
	public function cronMonth()
	{ return; }
	
	public function getViewPath()
	{
		global $katana;
		if((self::getCoreHandle() != '') && (self::getViewObject() != ''))
		{
			if(($katana->getList() != '') || (self::getList() != ''))
			{ $viewPath = VIEWS_INCLUDE_PATH.self::getCoreHandle().'/list/'; }
			else
			{ $viewPath = VIEWS_INCLUDE_PATH.self::getCoreHandle().'/detail/'; }
		}
		return $viewPath;
	}
	
	public function getViewTemplate()
	{
		global $katana;
		$template = '';
		if((self::getCoreHandle() != '') && (self::getViewObject() != ''))
		{
			if(($katana->getList() != '') || (self::getList() != ''))
			{
				//echo 'checking:'.TEMPLATES_PATH.'sites/'.$katana->getId().'/layout/views/'.self::getCoreHandle().'/list/'.self::getViewObject().'.php<br />';
				if(file_exists(TEMPLATES_PATH.'sites/'.$katana->getId().'/layout/views/'.self::getCoreHandle().'/list/'.self::getViewObject().'.php'))
				{ $template = TEMPLATES_PATH.'sites/'.$katana->getId().'/layout/views/'.self::getCoreHandle().'/list/'.self::getViewObject().'.php'; }
				else
				{ 
					//echo 'checking:'.VIEWS_INCLUDE_PATH.self::getCoreHandle().'/list/'.self::getViewObject().'.php<br />';
					if(file_exists(VIEWS_INCLUDE_PATH.self::getCoreHandle().'/list/'.self::getViewObject().'.php'))
					{ $template = VIEWS_INCLUDE_PATH.self::getCoreHandle().'/list/'.self::getViewObject().'.php'; }
				}
			}
			else
			{
				//echo 'checking:'.TEMPLATES_PATH.'sites/'.$katana->getId().'/layout/views/'.self::getCoreHandle().'/detail/'.self::getViewObject().'.php<br />';
				if(file_exists(TEMPLATES_PATH.'sites/'.$katana->getId().'/layout/views/'.self::getCoreHandle().'/detail/'.self::getViewObject().'.php'))
				{ $template = TEMPLATES_PATH.'sites/'.$katana->getId().'/layout/views/'.self::getCoreHandle().'/detail/'.self::getViewObject().'.php'; }
				else
				{ 
					//echo 'checking:'.VIEWS_INCLUDE_PATH.self::getCoreHandle().'/detail/'.self::getViewObject().'.php<br />';
					if(file_exists(VIEWS_INCLUDE_PATH.self::getCoreHandle().'/detail/'.self::getViewObject().'.php'))
					{ $template = VIEWS_INCLUDE_PATH.self::getCoreHandle().'/detail/'.self::getViewObject().'.php'; }
				}
			}
		}
		//echo '$template:'.$template;
		if(is_file($template))
		{ return $template; }
	}
	
	public function getSearchValue($key)
	{
		if(isset($this->Search[$key]))
		{ return $this->Search[$key]; }
	}
	
	public function getCountryTitle($iso = '')
	{
		if($iso != '')
		{
			$countries = self::getCountryList();
			if($countries[$iso])
			{ return $countries[$iso]['printable_name']; }
		}
	}
	
	public function getForm($key='')
	{
		if($key != '')
		{
			if(is_object($this->Forms[$key]))
			{ return $this->Forms[$key]; }
			else
			{ 
				$this->loadForms($key); 
				if(is_object($this->Forms[$key]))
				{ return $this->Forms[$key]; }
			}
		}
	}
	
	public function getForms($key='')
	{
		if($key != '')
		{ 
			$form = self::getForm($key);
			return $form;
		}
	}
	
	public function loadForms($key='')
	{ $this->Forms[$key] = new Form; }
	
	public function getSettings()
	{ 
		if(!self::getItemLoaded('settings'))
		{ self::loadSettings(); }
		return $this->Settings;
	}
	
	public function getSetting($setting='') 
	{ 
		if(!self::getItemLoaded('settings'))
		{ self::loadSettings(); }
		if(isset($this->Settings[$setting]))
		{ return $this->Settings[$setting]; }
	}
	
	function loadSettings()
	{ return; }
	
	public function getInfoField($field) 
	{ 
		if(isset($this->Info[$field]))
		{ return '<span class="info">'.$this->Info[$field].'</span>'; }
	}
	public function getErrorField($field) 
	{ 
		if(isset($this->Error[$field]))
		{ return '<span class="error">'.$this->Error[$field].'</span>'; }
	}
	
	public function addInfo($info, $val) 
	{ $this->Info[$info]=$val; }
	public function addError($error, $val) 
	{ $this->Error[$error]=$val; }
	
	function setTableActive($id, $table, $active)
	{
		$update  = 'UPDATE '.DB_PERFIX.$table.' SET ';
		$update .= 'active='.$active.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update); 
	}
	
	function setTableFieldValueOnId($table, $field, $value, $id)
	{
		$update  = 'UPDATE '.DB_PERFIX.$table.' SET ';
		$update .= $field.'='.$value.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
		
	}
	
	function setTableItemDefault($id, $table, $active, $defield)
	{
		$update  = 'UPDATE '.DB_PERFIX.$table.' SET ';
		$update .= ''.$defield.'='.$active.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	
	function makeTableItemsOrder($parentid, $table, $order_field='item_order', $parent_field='parent_id')
	{
		$order = 0;
		$sql = 'SELECT id, '.$order_field.' FROM '.DB_PERFIX.$table.' WHERE '.$parent_field.'='.$parentid.' ORDER BY '.$order_field.', id ASC;';
		$select = mysql_query($sql);
		while($row = mysql_fetch_array($select))
		{ 
			$sql = 'UPDATE '.DB_PERFIX.$table.' SET '.$order_field.'='.$order.' WHERE id='.$row['id'].' LIMIT 1;';
			$update = mysql_query($sql);
			$order++;
		}
		return $order;
	}
	
	function getNextTableItemOrder($parentid, $table, $order_field='item_order', $parent_field='parent_id')
	{
		$order = 0;
		$last = -1;
		$sql = 'SELECT '.$order_field.' FROM '.DB_PERFIX.$table.' WHERE '.$parent_field.'='.$parentid.' ORDER BY '.$order_field.' DESC LIMIT 1;';
		$select = mysql_query($sql);
		while($row = mysql_fetch_array($select))
		{ $last = $row[$order_field];  }
		if($last > -1)
		{ $order = ($last+1); }
		return $order;
	}
	
	function setTableItemOrderFirstTime($id, $table, $order_field='item_order', $parent_field='parent_id')
	{
		$orderval = 0;
		$sql = 'SELECT '.$order_field.', '.$parent_field.' FROM '.DB_PERFIX.$table.' WHERE id='.$id.';';
		$select = mysql_query($sql);
		while($row = mysql_fetch_array($select))
		{ 
			$up_order = $row[$order_field]; 
			$parent = $row[$parent_field]; 
		}
		
		$sql = 'SELECT id, '.$order_field.' FROM '.DB_PERFIX.$table.' WHERE '.$parent_field.'='.$parent.' ORDER BY '.$order_field.', id ASC;';
		$result = mysql_query($sql);
		while($row = mysql_fetch_array($result))
		{
			$current['id'] = $row['id'];
			$current[$order_field] = $row[$order_field];
			$sql = 'UPDATE '.DB_PERFIX.$table.' SET '.$order_field.'='.$orderval.' WHERE id='.$current['id'].' LIMIT 1;';
			$update = mysql_query($sql);
			$orderval++;
		}
	}
	
	function setTableItemOrder($id, $old_order, $set_order, $table, $order_field='item_order', $parent_field='parent_id')
	{
		if(($id > 0) && (is_numeric($old_order)) && (is_numeric($set_order)))
		{
			
			self::setTableItemOrderFirstTime($id, $table, $order_field, $parent_field);
			if($old_order < $set_order)
			{
				for($i=$old_order; $i<$set_order; $i++)
				{ self::setTableItemOrderDown($id, $table, $order_field, $parent_field); }
			}
			if($old_order > $set_order)
			{
				for($i=$old_order; $i>$set_order; $i--)
				{ self::setTableItemOrderUp($id, $table, $order_field, $parent_field); }
			}
			self::setTableItemOrderFirstTime($id, $table, $order_field, $parent_field); 
		}
	}
	
	function setTableItemOrderUp($id, $table, $order_field='item_order', $parent_field='parent_id', $firstrun = true)
	{
		$max = 0;
		$elements = 0;
		
		$sql = 'SELECT '.$order_field.', '.$parent_field.' FROM '.DB_PERFIX.$table.' WHERE id='.$id.';';
		$select = mysql_query($sql);
		while($row = mysql_fetch_array($select))
		{ 
			$up_order = $row[$order_field]; 
			$parent = $row[$parent_field]; 
		}
		$sql = 'SELECT id, '.$order_field.' FROM '.DB_PERFIX.$table.' WHERE '.$parent_field.'='.$parent.' ORDER BY '.$order_field.', id ASC;';
		$result = mysql_query($sql);
		$sql = '';
		while($row = mysql_fetch_array($result))
		{
			if($max < $row[$order_field])
			{ $max = $row[$order_field]; }
			$elements++;
			$current['id'] = $row['id'];
			$current[$order_field] = $row[$order_field];
			$down_order = $current[$order_field] + 1;
			if($down_order == $up_order)
			{
				$sql = 'UPDATE '.DB_PERFIX.$table.' SET '.$order_field.'='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;';
				$update = mysql_query($sql);
				$sql = 'UPDATE '.DB_PERFIX.$table.' SET '.$order_field.'='.$current[$order_field].' WHERE id='.$id.' LIMIT 1;';
				$update = mysql_query($sql);
				return;
			}
		}
		if(($sql == '') && ($elements > 1) && ($firstrun == true))
		{ 
			self::setTableItemOrderFirstTime($id, $table, $order_field, $parent_field); 
			self::setTableItemOrderUp($id, $table, $order_field, $parent_field, false);
		}
	}
	
	function setTableItemOrderDown($id, $table, $order_field='item_order', $parent_field='parent_id', $firstrun = true)
	{
		$min = 0;
		$elements = 0;
		
		$sql = 'SELECT '.$order_field.', '.$parent_field.' FROM '.DB_PERFIX.$table.' WHERE id='.$id.';';
		$select = mysql_query($sql);
		if(mysql_num_rows($select) > 0)
		{
			while($row = mysql_fetch_array($select))
			{ 
				$down_order = $row[$order_field]; 
				$parent = $row[$parent_field]; 
			}
		}
		$sql = 'SELECT id, '.$order_field.' FROM '.DB_PERFIX.$table.' WHERE '.$parent_field.'='.$parent.' ORDER BY '.$order_field.', id ASC;';
		$result = mysql_query($sql);
		$sql = '';
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_array($result))
			{
				if($min > $row[$order_field])
				{ $min = $row[$order_field]; }
				$elements++;
				$current['id'] = $row['id'];
				$current[$order_field] = $row[$order_field];
				$up_order = $current[$order_field] - 1;
				if($down_order == $up_order)
				{
					$sql = 'UPDATE '.DB_PERFIX.$table.' SET '.$order_field.'='.$up_order.' WHERE id='.$current['id'].' LIMIT 1;';
					$update = mysql_query($sql);
					$sql = 'UPDATE '.DB_PERFIX.$table.' SET '.$order_field.'='.$current[$order_field].' WHERE id='.$id.' LIMIT 1;';
					$update = mysql_query($sql);
					return;
				}
			}
		}
		if(($sql == '') && ($elements > 1) && ($firstrun == true))
		{ 
			self::setTableItemOrderFirstTime($id, $table, $order_field, $parent_field); 
			self::setTableItemOrderUp($id, $table, $order_field, $parent_field, false);
		}
	}
	
	function loadSearchValues()
	{ return; }
}
?>